package com.pap.rbac.userrole.agent.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.pap.base.service.impl.PapBaseServiceImpl;
import com.pap.base.util.date.DateUtils;
import com.pap.rbac.agent.userrole.IUserRoleRelAgent;
import com.pap.rbac.userrole.auto.entity.UserRoleRel;
import com.pap.rbac.userrole.auto.mapper.UserRoleRelMapper;

@Transactional
@Service("userRoleRelAgent")
public class UserRoleRelAgentImpl extends PapBaseServiceImpl<UserRoleRel> implements IUserRoleRelAgent {

	@Autowired
	private UserRoleRelMapper userRoleRelMapper;
	
	@Override
	public void updateUserRoleRel(String clientLicenseId, String userId, List<String> roleLists) {
		userRoleRelMapper.deleteByUserId(userId);
		if(roleLists != null) {
			for (String string : roleLists) {
				UserRoleRel userRoleRel = new UserRoleRel();
				userRoleRel.setId(UUID.randomUUID().toString());
				userRoleRel.setUserId(userId);
				userRoleRel.setRoleId(string);
				userRoleRel.setCreateIp("0.0.0.0");
				userRoleRel.setCreateTime(DateUtils.getCurrDateTimeStr());
				userRoleRel.setCreateUser(userId);
				userRoleRel.setClientLicenseId(clientLicenseId);
				userRoleRelMapper.insertSelective(userRoleRel);
			}
		}
	}

	@Override
	public List<String> selectUserRoleIds(String userId) {
		List<String> returnRoleIdsList = new ArrayList<String>();
		Map<Object, Object> selectUserIdMap = new HashMap<Object, Object>();
		selectUserIdMap.put("userId", userId);
		List<UserRoleRel> userRoleRelList = userRoleRelMapper.selectListByMap(selectUserIdMap);
		if (userRoleRelList != null) {
			for (UserRoleRel userRoleRel : userRoleRelList) {
				returnRoleIdsList.add(userRoleRel.getRoleId());
			}
		}
		return returnRoleIdsList;
	}

}
